/*
* D Time Clock.
* Author: Tandat
* Email: tandat@gmail.com, tidusvn05@gmail.com
* usage: $(obj).dContextMenu({time:'15:00:00'});
*/
(function($){
	var menu;
	
	//Attach this new method to jQuery
 	$.fn.dContextMenu = function(menu_data,doptions) {
		var options = $.extend($.fn.dContextMenu.defaults, doptions);
		
		if (!menu) {                                      // Create singleton menu
		      menu = $('<div id="d-context-menu"></div>')
		               .hide()
		               .css({position:'absolute', zIndex:'500'})
		               .appendTo('body')
		               .bind('click', function(e) {
		                 e.stopPropagation();
		               });
		 }
	    $(this).bind('click', function(e) {
	      // Check if onClick defined
	      var bShowContext = (menu.onClick) ? hash[index].onClick(e) : true;
	      if (bShowContext) display(menu_data, this, e, options);
	      return false;
	    });
	    return this;
	    
	};
	
	function display(menu_data, trigger, e, options) {
		var row_id = $(trigger).attr('href').replace("#","");
		var handle_function = options.handle;
	    var html ="<ul>";
		for (var item in menu_data) {
			console.log(item);
			html += "<li><a onclick="+handle_function+"('"+row_id+"','"+item+"') >" + menu_data[item] + "</a></li>";
		}
		html += "</ul>";
		//insert content
		menu.html(html);
		var left = e.pageX;
		var top = e.pageY;
		menu.css({'left':left,'top':top});
	    menu.show();
	    
	  $(document).one('click', hide);
	}
	
	function hide() {
	    menu.hide();
	}
	//default options, no se duoc merge voi params args
	$.fn.dContextMenu.defaults = {
		default_time: new Date(),
		separator: ':',
		
	};

//pass jQuery to the function, 
//So that we will able to use any valid Javascript variable name 
//to replace "$" SIGN. But, we'll stick to $ (I like dollar sign: ) )		
})(jQuery);